/*
** gal.h: graphic absract layer header file.
**
** Copyright (C) 2007-2008 SKY-MOBI AS.  All rights reserved.
**
** This file is part of the simple gui library.
** It may not be redistributed under any circumstances.
*/

#ifndef _SGL_GAL_H
#define _SGL_GAL_H

#include "sgl_types.h"

#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */

enum {
	GAL_RECTANGLE,
	GAL_RECTANGLE2,
	GAL_RECTANGLE_C,

	GAL_TEXTWIDTHHEIGHT,
	GAL_DRAWTEXTEX,
	GAL_DRAWTEXTEX2,
	GAL_DRAWTEXT,
	GAL_C2U,
	GAL_CHANGEPATH,
	GAL_SETDEFAULTPATH,
	GAL_READALL,

	GAL_FILLFRAME_C,
	
	GAL_MAX

};

typedef struct _screendevice
{
	mr_screeninfo scrInfo;
}* GAL_GC;


/**
 * \brief Normal colors.
 */
#define COLOR_transparent	0x00000000
#define COLOR_darkred		0x00800000
#define COLOR_darkgreen		0x00008000
#define COLOR_darkyellow	0x00808000
#define COLOR_darkblue		0x00000080
#define COLOR_darkmagenta	0x00800080
#define COLOR_darkcyan		0x00008080
#define COLOR_lightgray		0x00c0c0c0
#define COLOR_darkgray		0x00808080
#define COLOR_red			0x00ff0000
#define COLOR_green			0x0000ff00
#define COLOR_yellow			0x00ffff00
#define COLOR_blue			0x000000f0
#define COLOR_magenta		0x00ff00ff
#define COLOR_cyan			0x0000ffff
#define COLOR_lightwhite		0x00ffffff
#define COLOR_black			0x00000000

/**
 * \brief The phisical graphic context handle.
 */

#if defined(INCLUDE_IN_PLUG_SGL_SRC) || defined(SDK_NO_PLUGIN)

#define PHYSICALGC	(&scrdev)
extern struct _screendevice scrdev;

/**
 * \brief Intialize the graphic abstract layer.
 */
int32 GAL_Initialize (VOID);

/**
 * \brief Terminate the graphic abstract layer.
 */
int32 GAL_Terminate (VOID);
#endif

/**
 * \brief Get the bytes per pixel.
 */
#define GAL_BytesPerPixel(gc) \
	DIV((gc)->scrInfo.bit + 7 , 8)

/**
 * \brief Get the bits per pixel.
 */
#define GAL_BitsPerPixel(gc) \
	((gc)->scrInfo.bit)

/**
 * \brief Get the screen width.
 */
#define GAL_Width(gc) \
	((gc)->scrInfo.width)

/**
 * \brief Get the screen height.
 */
#define GAL_Height(gc) \
	((gc)->scrInfo.height)

/**
 * \brief Get supported colors.
 */
#define GAL_Colors(gc) \
	(((gc)->scrInfo.bit >= 24)? (1 << 24): (1 << (gc)->scrInfo.bit))

/**
 * \brief Fill the box with a specific color.
 */

#define GAL_FillBox(gc, x, y, w, h, pixel) \
	mrc_drawRect((int16)(x), (int16)(y), (int16)(w), (int16)(h), PIXEL888RED(pixel), PIXEL888GREEN(pixel), PIXEL888BLUE(pixel))

/*
#define GAL_FillBox(gc, x, y, w, h, pixel) \
	GAL_FillBox_A((int16)(x), (int16)(y), (int16)(w), (int16)(h), pixel)
*/

 
/**
 * \brief Draw a horizontal line.
 */
#define GAL_DrawHLine(gc, x, y, w, pixel) \
	mrc_drawLine((int16)(x), (int16)(y), (int16)((x)+(w)-1), (int16)(y), PIXEL888RED(pixel), PIXEL888GREEN(pixel), PIXEL888BLUE(pixel))

/**
 * \brief Draw a vertical line.
 */
#define GAL_DrawVLine(gc, x, y, h, pixel) \
	mrc_drawLine((int16)(x), (int16)(y), (int16)(x), (int16)((y)+(h)-1), PIXEL888RED(pixel), PIXEL888GREEN(pixel), PIXEL888BLUE(pixel))

/**
 * \brief Draw a pixel.
 */
#define GAL_DrawPixel(gc, x, y, pixel) \
	mrc_drawPointEx((int16)(x), (int16)(y), PIXEL888RED(pixel), PIXEL888GREEN(pixel), PIXEL888BLUE(pixel))

/**
 * \brief Daw a normal line.
 */
#define GAL_Line(gc, x1, y1, x2, y2, pixel) \
	mrc_drawLine((int16)(x1), (int16)(y1), (int16)(x2), (int16)(y2), PIXEL888RED(pixel), PIXEL888GREEN(pixel), PIXEL888BLUE(pixel))

/**
 * \brief Flush all the screen.
 */
#define GAL_Flush(gc) \
	mrc_refreshScreen(0, 0, (uint16)(gc->scrInfo.width), (uint16)(gc->scrInfo.height))

/**
 * \brief Flush a region of the screen.
 */
#define GAL_FlushRegion(gc,  x,  y,  w, h) \
	mrc_refreshScreen((int16)(x), (int16)(y), (uint16)(w), (uint16)(h))

#if defined(INCLUDE_IN_PLUG_SGL_SRC) || defined(SDK_NO_PLUGIN)
int32 GAL_DrawBackGround(int16 x, int16 y, int16 w, int16 h);
/**
 * \brief Draw a rectangle.
 */
int32 GAL_Rectangle(GAL_GC gc, int x, int y, int w, int h, Uint32 pixel);

/**
 * \brief Draw a rectangle with a specific width.
 */
int32 GAL_Rectangle2(GAL_GC gc, int x, int y, int w, int h, int line, Uint32 pixel);

int32 GAL_RectangleC(GAL_GC gc, int x, int y, int w, int h, gal_pixel pixel);
int32 GAL_FillFrameC(int x, int y, int w, int h, gal_pixel pixel, gal_pixel fillColor);
 
int32 GAL_FillBox_A(int16 x, int16 y, int16 w, int16 h,int32 pixel);

/**
 * \brief Same as mrc_textWidthHeight()
 */
int32 GAL_textWidthHeight(char* pcText, int is_unicode, uint16 font, int32* w, int32* h);
int32 GAL_drawTextEx(uint8* pcText, PTXTRECT pRect, uint32 color, int flag, uint16 font);
int32 GAL_drawText(uint8* pcText, int16 x, int16 y,uint32 color, int isUnicode, uint16 font);
uint16* GAL_c2u(char *cp, int32 *err, int32 *size);

int32 GAL_changePath(char* path);
int32 GAL_setDefaultPath(void);
uint8* GAL_readAll(const char* filename, uint32* len, int32 encrypt);


#else
/**
 * \brief Draw a rectangle.
 */
#define GAL_Rectangle(gc, x, y, w, h, pixel) \
		mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_RECTANGLE, (int32)x, (int32)y, (int32)w, (int32)h, (int32)pixel)


/**
 * \brief Draw a rectangle with a specific width.
 */
#define GAL_Rectangle2(gc, x, y, w, h, line, pixel) \
	do { \
		RECT rect; \
		rect.left = x; rect.top = y; rect.width = w; rect.height = h; \
		mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_RECTANGLE2, (int32)&rect, (int32)line, (int32)pixel, (int32)0, (int32)0); \
	} while(0);

#define GAL_RectangleC(gc, x, y, w, h, pixel) \
		mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_RECTANGLE_C, (int32)x, (int32)y, (int32)w, (int32)h, (int32)pixel)
/**
 * \brief Same as mrc_textWidthHeight()
 */
#define GAL_textWidthHeight(pcText, is_unicode, font, w, h) \
	(int32)mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_TEXTWIDTHHEIGHT, (int32)pcText, (int32)is_unicode, (int32)font, (int32)w, (int32)h)
	
#define GAL_drawTextEx(pcText, pRect, color, flag, font) \
	(int32)mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_DRAWTEXTEX, (int32)pcText, (int32)pRect, (int32)color, (int32)flag, (int32)font)

#define GAL_drawText(pcText, x, y,color, isUnicode, font) \
	(int32)mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_DRAWTEXT, (int32)pcText, (int32)((x&0xffff)<<16) |(y&0xffff), (int32)color, (int32)isUnicode, (int32)font)

#define GAL_c2u(cp, err, size) \
	(uint16 *)mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_C2U, (int32)cp, (int32)err, (int32)size, (int32)0, (int32)0)

#define GAL_changePath(path) \
	mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_CHANGEPATH, (int32)path, (int32)0, (int32)0, (int32)0, (int32)0)
		
#define GAL_setDefaultPath() \
	mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_SETDEFAULTPATH, (int32)0, (int32)0, (int32)0, (int32)0, (int32)0)
		
#define GAL_readAll(filename, len, encrypt) \
	(uint8 *)mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_READALL, (int32)filename, (int32)len, (int32)encrypt, (int32)0, (int32)0)

#define GAL_FillFrameC(x, y, w, h, pixel, fillColor) \
	do { \
		RECT rr; \
		rr.left = x; rr.top = y; rr.width = w; rr.height = h; \
		mrc_extSendAppEventEx(MPS_CODE_GAL, GAL_FILLFRAME_C, (int32)&rr, (int32)pixel, (int32)fillColor, (int32)0, (int32)0); \
	} while(0);

#define SGL_LoadPlugin() \
	mrc_extSendAppEventEx(MPS_CODE_SGL_LOADPLUGIN, (int32)0, (int32)0, (int32)0, (int32)0, (int32)0, (int32)0)
	

#endif

#define GAL_drawTextEx2(pcText, x, y, r, color, flag, font) \
	do { \
		TXTRECT txtRect; \
		txtRect.startX = x; txtRect.startY = y; txtRect.rect = &r; \
		GAL_drawTextEx(pcText, &txtRect, color, flag, font); \
	} while(0);

#ifdef __cplusplus
}
#endif  /* __cplusplus */

#endif /* _SGL_GAL_H */

